var express = require('express')
var router = express.Router()
var Students = require('./student')
var Utils = require('./utils')
var neo4j = require('neo4j-driver').v1;
var fs = require('fs')
var dbpath = './public/json/output.json'

var driver = neo4j.driver("bolt://localhost:7687", neo4j.auth.basic("neo4j", "1991112230li"));
const session = driver.session();

// 渲染查询化学物质页面
router.get('/test', function (req, res) {
    res.render('test.html')
})


// 处理查询物质请求
router.post('/test', function (req, res) {
    console.log(req.body)  //{ name: 'HCl' }
    const readTxPromise = session.readTransaction(tx => tx.run(`match(name:ns0__NamedIndividual{ns1__化学式:"${req.body.name}"})--(individual:ns0__NamedIndividual)return individual,name`));
    readTxPromise.then(result => {
        session.close();
        if (result) {
            console.log('********************')
            console.log(result)
            let json = { "records": result.records }
            var fileData = JSON.stringify(Utils.dataTransform(json))
            fs.writeFile(dbpath, fileData, function (err) {
                if (err) throw err
            })
            res.status(200).json({
                err_code: 0,
                message: 'OK'
            })

            // res.render('test.html', {
            //     records: result.records
            // })
        }
    })
})





// (n:`ns1__气体`) RETURN n LIMIT 25
// 渲染首页
router.get('/students', function (req, res) {
    Students.find(function (err, data) {
        if (err) {
            return res.status(500).send('server error')
        }
        res.render('index.html', {
            fruits: [{ name: "苹果", season: "春" }, { name: "苹果", season: "春" }, { name: "苹果", season: "春" }, { name: "苹果", season: "春" }],
            students: data
        })
    })
})
// 渲染添加学生页面
router.get('/students/new', function (req, res) {
    res.render('new.html')
})
//处理添加学生post请求 post请求最终有个redict
router.post('/students/new', function (req, res) {
    var student = new Students(req.body)
    student.save(function (err) {
        if (err) {
            return res.status(500).send('server error')
        }
        res.redirect('/students')
    })

})

//渲染学生个人页面通过ID
router.get('/students/edit', function (req, res) {
    //req.query.id.replace(/"/g,'') //把" 全部替换为空 去掉"
    Students.findById(req.query.id, function (err, data) {
        if (err) {
            return res.status(500).send('server error')
        }
        console.log(data);//
        res.render('edit.html', {
            student: data //{}
        })

    })


})
//处理编辑请求
router.post('/students/edit', function (req, res) {
    // console.log(req.body.id);
    Students.findByIdAndUpdate(req.body.id, req.body, function (err) {
        if (err) {
            return res.status(500).send('server error')

        }
        res.redirect('/students')
    })
})
//删除
router.get('/students/delete', function (req, res) {
    Students.findByIdAndRemove(req.query.id, function (err) {
        if (err) {
            return res.status(500).send('server error')
        }
        res.redirect('/students')
    })
})

//导出单个组件用module.exports 导出多个用exports.XXX=XXX
module.exports = router

